package com.gx.smart.construction.ui.house.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.base.BaseMVVMRefreshFragmentV2;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.SmartHomeRecordFragmentBinding;
import com.gx.smart.construction.ui.house.adapter.RecordItemAdapter;
import com.gx.smart.construction.ui.house.model.RecordType;
import com.gx.smart.construction.ui.house.viewmodel.SmartHomeRecordViewModel;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfo;
import com.mylhyl.circledialog.CircleDialog;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class SmartHomeRecordFragment extends BaseMVVMRefreshFragmentV2<SmartHomeRecordViewModel>
    implements BaseAdapter.OnItemLongClickListener<ConstructionAppRoomInfo>{
    @Inject
    RecordItemAdapter adapter;
    private SmartHomeRecordFragmentBinding dataBinding;
    private final List<RecordType> items = new ArrayList<>();
    private int type = 3;

    @Override
    public int onBindLayout() {
        return R.layout.smart_home_record_fragment;
    }

    @Override
    public String getToolbarTitle() {
        return "添加记录";
    }

    @Override
    protected Class<SmartHomeRecordViewModel> onBindViewModel() {
        return SmartHomeRecordViewModel.class;
    }

    @Override
    public void initContentView(ViewGroup root) {
        dataBinding = DataBindingUtil.inflate(getLayoutInflater(), onBindLayout(), root, true);
        dataBinding.setLifecycleOwner(getViewLifecycleOwner());
        dataBinding.setSmartHomeRecordFragment(this);
        dataBinding.setSmartHomeRecordViewModel(viewModel);
        adapter.setOnItemLongClickListener(this);
        dataBinding.recyclerView.setAdapter(adapter);
        dataBinding.recyclerView.setHasFixedSize(true);
        initView(dataBinding.recyclerView, adapter, dataBinding.refreshLayout);

        DividerItemDecoration divider = new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL);
        @SuppressLint("UseCompatLoadingForDrawables")
        Drawable drawable = getResources().getDrawable(R.drawable.shape_list_item_divider_line);
        divider.setDrawable(drawable);
        dataBinding.recyclerView.addItemDecoration(divider);
    }

    @Override
    protected void initData() {
        RecordType item1 = new RecordType();
        item1.setType(3);
        item1.setContent("全部");
        items.add(item1);

        RecordType item2 = new RecordType();
        item2.setType(1);
        item2.setContent("未绑定");
        items.add(item2);


        RecordType item3 = new RecordType();
        item3.setType(2);
        item3.setContent("已绑定");
        items.add(item3);
        dataBinding.refreshLayout.autoRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        viewModel.recordList(currentPage, type);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        super.onLoadMore(refreshLayout);
        viewModel.recordList(currentPage, type);
    }

    @SuppressWarnings("unused")
    public void selectRoomType(View v) {
        new CircleDialog.Builder()
                .setItems(items, (parent, view, position, id) -> {
                    RecordType item = items.get(position);
                    type = item.getType();
                    viewModel.setSelectedType(item.getContent());
                    dataBinding.refreshLayout.autoRefresh();
                    return true;
                })
                .setNegative("取消", null)
                .show(requireActivity().getSupportFragmentManager());
    }

    @Override
    protected void initObserver() {
        super.initObserver();
        viewModel.data.observe(this, list -> handleResult(list));
    }


    @Override
    public boolean onItemLongClick(ConstructionAppRoomInfo item, int position) {
        Logger.e("onItemLongClick position:"+position);
        adapter.remove(position);
        adapter.notifyItemRemoved(position);
        adapter.notifyItemRangeChanged(position, adapter.getItemCount() - position);
        return true;
    }
}